package com.google.code.p.ivans;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cafe.ejb.WaiterService;
import cafe.ejb.entities.Waiter;
import cafe.ejb.myexceptions.EntityNotFoundException;

public class ChangeWaiter extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	@EJB 
	private WaiterService waiterSI;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		try {
			
			int id = Integer.valueOf(request.getParameter("id"));
			
			String firstName = new String(request.getParameter("firstName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			String lastName = new String(request.getParameter("lastName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			String patronymicName = new String(request.getParameter("patronymicName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			int salary = Integer.valueOf(request.getParameter("salary"));
					
			
			if(!firstName.equals("")) {
				waiterSI.updateWaiter(id, Waiter.FIRSTNAME_UPDATE, firstName);
			}			
			if(!lastName.equals("")) {
				waiterSI.updateWaiter(id, Waiter.LASTNAME_UPDATE, lastName);
			}
			if(!patronymicName.equals("")) {
				waiterSI.updateWaiter(id, Waiter.PATRONYMICNAME_UPDATE, patronymicName);
			}
			if(salary > 0) {
				waiterSI.updateWaiter(id, Waiter.SALARY_UPDATE, salary);
			}
			
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("successfulWaiterChanging.jsp");
			
			requestDispatcher.forward(request, response);
			
		} catch (NumberFormatException e) {
			
		} catch (EntityNotFoundException e) {
			
		} catch (SQLException e) {
			
		}
	}

}
